package com.hdoit.kylin.logical.baseInfo.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.tennetcn.common.dao.base.impl.SuperService;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.logical.superbase.model.Role;
import com.tennetcn.common.logical.superbase.service.IEmployeeService;
import com.tennetcn.common.logical.superbase.service.IRoleService;
import com.tennetcn.common.message.DaoBaseRuntimeException;
import com.tennetcn.common.utils.StringHelper;
import com.tennetcn.plugins.devshorts.enumerable.Enumerable;
import com.hdoit.kylin.logical.baseInfo.dao.IOperationDao;
import com.hdoit.kylin.logical.baseInfo.model.Operation;
import com.hdoit.kylin.logical.baseInfo.service.IOperationService;
/**
 * @author      lufu
 * @email       
 * @createtime  2017年07月27日 16:50:58
 * @comment
 */
@Component
public class OperationServiceImpl extends SuperService<Operation> implements IOperationService{
	private IOperationDao operationDao;

	public IOperationDao getOperationDao() {
		return operationDao;
	}

	@Resource
	public void setOperationDao(IOperationDao operationDao) {
		this.operationDao = operationDao;
	}
	
	private IEmployeeService employeeService;
	private IRoleService roleService;

	public IEmployeeService getEmployeeService() {
		return employeeService;
	}

	@Resource
	public void setEmployeeService(IEmployeeService employeeService) {
		this.employeeService = employeeService;
	}

	public IRoleService getRoleService() {
		return roleService;
	}

	@Resource
	public void setRoleService(IRoleService roleService) {
		this.roleService = roleService;
	}
	
	@Override
	public boolean applyChange(Operation operation) {
		if(employeeService.applyEmployeeLogin(operation)){
			if(ModelStatus.add.equals(operation.getModelStatus())){
				List<Role> roles=roleService.queryListByMark("operation");
				List<String> roleIdList=Enumerable.init(roles).map(role->role.getRoleId()).toList();
				
				if(!employeeService.applyRoleIds(operation, StringHelper.join(roleIdList))){
					throw new DaoBaseRuntimeException("applyRoleIds roll back");
				}
			}
			
			if(!super.applyChange(operation)){
				throw new DaoBaseRuntimeException("teacher applyChange roll back");
			}
			return true;
		}
		return false;
	}

}